package cn.itsource.pet.service.impl;

import cn.itsource.basic.domain.Point;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.DistanceUtil;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private ShopMapper shopMapper;
    // 保存一条数据
    @Override
    public void publish(SearchMasterMsg searchMasterMsg) {
        //1.发布消息的用户为空(controller 里面解决)
        //2.计算离用户最近的门店
        /*
            2.1 把用户发布的地址转换为 经纬度
            2.2 把所有的门店加载出来 把所有的地址 都转换为经纬度
            2.3 求发布人与 门店之间的距离（计算 球面上两个点之间的距离）

         */
        //用户发布地址的经纬度
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        List<Shop> shops = shopMapper.loadAll();

        //计算最近的门店
        Shop shop = DistanceUtil.getNearestShop(point, shops);
        searchMasterMsg.setShop_id(shop.getId());

        //3.默认的状态 待处理
        searchMasterMsg.setState(0);
        super.add(searchMasterMsg);
    }
}
